<?php

namespace app\common\model\tysj;

class Store extends CUBase {
	protected $privacyFields = ['status', 'examine', 'updatetime', 'back_url', 'remark'];
    protected $append = ['type_text', 'spreadid'];

	protected static function init()
    {
        parent::init();

        // 注册 before_insert 事件
        self::beforeInsert(function ($data) {
        	$status = loadEnum('StoreStatus');
        	$examine = loadEnum('StoreExamine');
        	$data['sub_mchid'] = '';
            $data['status'] = $status->hideStatus();
            $data['examine'] = $examine->code2key('daishenhe');
            $data['back_url'] = '';
            $data['remark'] = '';
            return $data;
        });
    }

    //重置店铺状态
    public function reset($id = null) {
    	$status = loadEnum('Status');
        $examine = loadEnum('StoreExamine');
    	if(!$id) {
    		$id = $this->id;
    	}
    	$data = [];
    	$data['status'] = $status->hideStatus();
        $data['examine'] = $examine->prepare();
    	$this->where(['id' => $id])->update($data);
    	return $this;
    }

    //返回店铺类型文字
    public function getTypeTextAttr($value, $data) {
        $typeEnum = loadEnum('StoreType');
        if(!isset($data['type'])) {
            return '未查询';
        }
        return $typeEnum->get_options()[$data['type']];
    }

    //返回文字的是否支持发票
    public function getInvoiceTextAttr($value, $data) {
        $invoice = loadEnum('Invoice');
        return $invoice->get_options()[$data['is_invoice']];
    }

    //返回状态文本信息
    public function getStatusTextAttr() {
        $status = loadEnum('StoreStatus');
        $options = $status->get_options();
        return $options[$this->getData($this->statusKey)];
    }

    //返回审核状态文本信息
    public function getExamineTextAttr() {
        $examine = loadEnum('StoreExamine');
        $options = $examine->get_options();
        return $options[$this->getData($this->examineKey)];
    }

    //绑定所属用户
    public function user() {
        return $this->hasOne(loader()->classModel('tyyh\\User'), 'id', 'uid');
    }
	
	//店铺法人身份证信息
	public function corporation() {
		return $this->belongsTo('Corporation', 'id', 'sid');
	}

	//店铺营业执照
	public function businessLicense() {
		return $this->belongsTo('businessLicense', 'id', 'sid');
	}

	//店铺绑定银行卡
	public function storeBank() {
		return $this->belongsTo('storeBank', 'id', 'sid');
	}

    //店铺推广人信息
    public function getSpreadidAttr($value, $data) {
        if(isset($data['spreadid'])) {
            return $data['spreadid'];
        }
        $row = loadModel('tysj\\StoreSpread')->where(['sid' => $data['id']])->find();
        return $row ? $row['spreadid'] : '';
    }

    //获取微信支付签约二维码
    public function getBackEwmAttr($value, $data) {
        if($data['back_url'] == '') {
            return '';
        }
        $qrCode = \Endroid\QrCode\QrCode::create($data['back_url'])
            ->setSize(300)
            ->setMargin(10);

        // 创建写入器
        $writer = new \Endroid\QrCode\Writer\PngWriter();
         // 生成保存路径，这里以当前时间戳作为文件名，保存到 uploads/qrcodes 目录下
        $savePath = ROOT_PATH . 'public/uploads/qrcodes/';
        if (!is_dir($savePath)) {
            mkdir($savePath, 0777, true);
        }
        $fileName = time() . '_' . $data['id'] . '.png';
        $fullPath = $savePath . $fileName;
        // 保存二维码到指定路径
        $result = $writer->write($qrCode);
        $result->saveToFile($fullPath);
        // 返回保存后的二维码相对路径
        return '/uploads/qrcodes/' . $fileName;
    }

    //获取店铺口碑值
    public function getStoreReputationAttr($value, $data) {
        $row = loadModel('tysj\\StoreReputation')->where('sid', $data['id'])->find();
        return $row ? $row['score'] : 0;
    }
}
